package com.yonyou.iuap.jxzy.service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.sql.visitor.functions.Length;
import com.yonyou.iuap.jxzy.dao.CheckWaterMapper;
import com.yonyou.iuap.jxzy.utils.FileUtils;

/**
 * 测水服务
 * @author WXF
 *
 */
@Service
public class CheckWaterService {
	//读文件路径
	private static final String FILE_NAME = "D:\\csydata";
	//写文件路径
	private static final String WRITER_FILE_NAME = "D:\\csydata_read";
	
	@Autowired
	private CheckWaterMapper checkWaterMapper;
	
	@Transactional
	public void save() throws IOException{
		File file = new File(FILE_NAME);
    	File[] fileList = file.listFiles();
    	StringBuffer erreyMsg = new StringBuffer();
    	for (int i = 0; i < fileList.length; i++) {
    		if (fileList[i].isFile()) {//只操作文件不操作路径
    			String fileName = fileList[i].getName();
//    			System.out.println("开始进行对文件操作：fileName =" + fileName);
    			try {
    				String stest = FileUtils.getFileText(fileList[i], "utf-8");
    				if(stest != null && stest.contains("#start_of_file") && stest.contains("#end_of_file")){
    					String licenseplateno = stest.substring(stest.indexOf("#plate_number")+"#plate_number".length(), 
    							stest.indexOf("#width_of_load"));//车牌号
    					if(licenseplateno!=null && licenseplateno.length()>0){
    						//车牌号转换
        					String fisvehicle=this.checkWaterMapper.queryFisvehicleByNickname(licenseplateno.substring(0, 2));
        					if(fisvehicle==null){
        						throw new Exception("根据车牌号字母【"+licenseplateno.substring(0, 2)+"】查询车牌号对照表为空，请检查！");
        					}
        					licenseplateno=fisvehicle+licenseplateno.substring(2);
        					licenseplateno=licenseplateno.toUpperCase();//变大写
    					}else{
    						return;
    					}
    					//车辆宽度
    					String width=stest.substring(stest.indexOf("#width_of_load")+"#width_of_load".length(), 
    							stest.indexOf("#checkwatertime"));//宽度 //#paper_type TODO范围错误pjx 19/07/19 15:26
    					String averagewaterratio = stest.substring(stest.indexOf("#average")+"#average".length(), 
    							stest.indexOf("#median"));//平均水分
    					String pk_poundbill = this.checkWaterMapper.findPoundbillByVvehicle(licenseplateno);
    					if(pk_poundbill == null || pk_poundbill.length() == 0){
    						throw new Exception("根据车牌号【"+licenseplateno+"】获取磅单数据为空");
    					}
    					String checkwatertime=null;
    					if(fileName!=null){
    						/*String[] filenames=fileName.split("_");
    						if(filenames!=null && filenames.length>=7){*/
    							checkwatertime=stest.substring(stest.indexOf("#checkwatertime")+"#checkwatertime".length(),
    									stest.indexOf("#paper_type"));//filenames[3]+":"+filenames[4]+":"+filenames[5];
    						//}
    					}
    					//插入测水数据
    					this.checkWaterMapper.saveCheckWater(pk_poundbill, licenseplateno, averagewaterratio,checkwatertime);
    					//将车辆宽度存在磅单表体vbdef12
    					this.checkWaterMapper.updatePoundWidth(width,pk_poundbill);
    					FileUtils.writerFile(stest,fileName, WRITER_FILE_NAME+getDateString()+"\\");
    					fileList[i].delete();//删除读取完成的文件
    				}
				} catch (Exception e) {
					erreyMsg.append("@@--文件名：fileName" + fileName + " 问题原因：" + e.getMessage()+";");
				}
    		}
    	}
    	System.out.println(erreyMsg);
	}
	
	public String getDateString(){
		Date d = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        return sdf.format(d);  
	}
	
	public static void main(String[] args) {
		String str = "#start_of_file#identifier2#plate_numberZFD8053#width_of_load230#paper_typeOCC C#moisture16.6317.1819.8618.2817.0016.3515.9116.1412.61#average16.7#median16.63#minimum12.61#maximum19.86#deviation1.84#replyip164.70.12.6#replyport9090#end_of_file";
		System.out.println(str.startsWith("#start_of_file"));
		System.out.println(str.endsWith("#end_of_file"));
		System.out.println(str.subSequence(str.indexOf("#plate_number")+"#plate_number".length(), str.indexOf("#width_of_load")));
		System.out.println(str.subSequence(str.indexOf("#average")+"#average".length(), str.indexOf("#median")));
	}
}
